﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.Model;
using IHISCETeim.Library;
using IHISCETeim.BLL;

namespace IHISCETeim.Web.MachineSys.MacModel
{
    public partial class OInspectionEdit : PageBase
    {
        protected override string DWZRef
        {
            get { return "OInspectionList"; }
        }


        /// <summary>
        /// 需查看的机号
        /// </summary>
        protected string SerialNumber
        {
            get
            {
                return GetRequestValue<string>("SerialNumber");
            }
        }

        private OInspectionInfo _obj;
        protected OInspectionInfo obj
        {
            get
            {
                if (_obj == null)
                    _obj = OInspectionController.GetList(ID);
                return _obj;
                //OMenuController.ge
            }
        }
        protected string t
        {
            get
            {
                return GetRequestValue<string>("t", "1");
            }
        }
        protected string SourceType
        {
            get
            {
                if (t == "1") return "完检单";
                if (t == "2") return "出库检查单";
                return "完检单";
            }
        }
        protected int ID
        {
            get
            {
                return GetRequestValue<int>("ID", 0);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "edit")
            {
                addmenu();
                
            }
            this.rpDJNGItem.DataSource = OInspectionController.GetNGItemList(ID, "完检单");
            this.rpDJNGItem.DataBind();
        }

        protected void addmenu()
        {
            string[] DJNGItemCodes = GetRequestValue<string>("DJNGItemCode", "").Split(new char[] { ',' });
            string[] DJNGExceptionrecords = GetRequestValue<string>("DJNGExceptionrecord", "").Split(new char[] { ',' });
            string[] PNOS = GetRequestValue<string>("PNO", "").Split(new char[] { ',' });
            string[] CodeTagS = GetRequestValue<string>("CodeTag", "").Split(new char[] { ',' });
            string[] StationS = GetRequestValue<string>("Station", "").Split(new char[] { ',' });
            string[] SupplierS = GetRequestValue<string>("Supplier", "").Split(new char[] { ',' });
            string[] NGClassS = GetRequestValue<string>("NGClass", "").Split(new char[] { ',' });
            string[] DutyWorkers = GetRequestValue<string>("DutyWorker", "").Split(new char[] { ',' });
            OInspectionInfo obj2 = Request.SerializableObject<OInspectionInfo>();
            obj2.OInspectionID = ID;
            obj2.SourceType = SourceType;
            List<ONGInspectionInfo> ngitems = new List<ONGInspectionInfo>();

            for (int index = 0; index < DJNGExceptionrecords.Count(); index++)
            {
                ONGInspectionInfo ngitem = new ONGInspectionInfo();
                if (!string.IsNullOrEmpty(DJNGExceptionrecords[index]))
                {
                    //ngitem.ItemCode = DJNGItemCodes[index];
                    ngitem.Exceptionrecord = DJNGExceptionrecords[index];
                    //ngitem.ItemType = 0;
                    ngitem.SerialNumber = obj2.SerialNumber;
                    //ngitem.PNO = PNOS[index];
                    // ngitem.CodeTag = CodeTagS[index];
                    ngitem.Station = StationS[index];
                    ngitem.Supplier = SupplierS[index];
                    ngitem.MachineType = obj2.MachineType;
                    ngitem.BillDate = obj2.CheckDate;
                    ngitem.SourceType = "完检单";
                    ngitem.NGClass = NGClassS[index];
                    ngitem.DutyWorker = DutyWorkers[index];
                    ngitems.Add(ngitem);
                }
            }

            this.AddLog(Permission.修改, obj, obj2);

            OInspectionController.Delete(obj2.OInspectionID);

            int r = OInspectionController.Add(obj2, ngitems);
            switch (r)
            {
                case -1:
                    ErrorJson("失败,机号已存在，不能重复添加．");
                    break;
                case -2:
                    ErrorJson("失败,仪表编号已存在，不能重复添加．");
                    break;
                default:
                    SuccessJson("成功");
                    break;
            }
        }
    }
}